package com.pencilisland.pay.mgt.console.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.pencilisland.pay.mgt.console.entity.SysAuth;
import com.pencilisland.pay.mgt.console.entity.SysMenu;
import com.pencilisland.pay.mgt.console.entity.SysOrg;
import com.pencilisland.pay.mgt.console.entity.SysRole;
import com.pencilisland.pay.mgt.console.entity.SysUser;
import com.pencilisland.pay.mgt.console.repository.AuthRepository;
import com.pencilisland.pay.mgt.console.service.AuthService;
import com.pencilisland.pay.mgt.console.service.OrgService;
import com.pencilisland.pay.mgt.console.service.RoleService;
import com.pencilisland.pay.mgt.util.G;

@Component
public class AuthServiceImpl implements AuthService {
	@Autowired
	AuthRepository authRepo;
	@Autowired
	OrgService orgService;
	@Autowired
	RoleService roleService;

	@Override
	public String load(Long pobjId, String pobjType, String pType) {
		List<SysAuth> auths = authRepo.findByPObjIdAndPObjTypeAndPType(pobjId, pobjType, pType);

		StringBuilder menuIds = new StringBuilder();
		if (auths != null && !auths.isEmpty()) {
			for (SysAuth auth : auths) {
				if (menuIds.length() == 0) {
					menuIds.append(auth.getMenuId());
				} else {
					menuIds.append(",").append(auth.getMenuId());
				}
			}
		}
		return menuIds.toString();
	}

	@Override
	@Transactional
	public void save(Long pobjId, String pobjType, String pType, String menuIds) {

		authRepo.delByPObjIdAndPObjTypeAndPType(pobjId, pobjType, pType);
		if (!StringUtils.isEmpty(menuIds)) {
			List<SysAuth> auths = new ArrayList<SysAuth>();
			String[] menuIdArray = menuIds.split(",");
			for (int i = 0; i < menuIdArray.length; i++) {
				SysAuth auth = new SysAuth();
				auth.setMenuId(G.l(menuIdArray[i]));
				auth.setpObjId(pobjId);
				auth.setpObjType(pobjType);
				auth.setpType(pType);
				auths.add(auth);
			}

			authRepo.save(auths);
		}
	}

	@Override
	public Boolean isAuth(SysMenu menu, SysUser user) {
		if ("admin".equals(user.getLoginName())) {
			return true;
		}

		Boolean isAuth = false;

		// 判断用户权限
		List<SysAuth> userAuths = authRepo.findByPObjIdAndPObjTypeAndMenuId(user.getUserId(), "U", menu.getMenuId());
		if (userAuths != null && !userAuths.isEmpty()) {
			for (SysAuth auth : userAuths) {
				if ("Y".equals(auth.getpType())) {
					isAuth = true;
				} else if ("N".equals(auth.getpType())) {
					isAuth = false;
					return isAuth;
				}
			}
		}

		// 判断用户所在组权限
		List<SysOrg> orgList = orgService.findAllParentOrg(user);
		if (orgList != null && !orgList.isEmpty()) {
			for (SysOrg org : orgList) {
				List<SysAuth> orgAuths = authRepo.findByPObjIdAndPObjTypeAndMenuId(org.getOrgId(), "G",
						menu.getMenuId());
				for (SysAuth auth : orgAuths) {
					if ("Y".equals(auth.getpType())) {
						isAuth = true;
					} else if ("N".equals(auth.getpType())) {
						isAuth = false;
						return isAuth;
					}
				}
			}
		}

		// 判断角色所在权限
		List<SysRole> roleList = roleService.findAllRoles(user, orgList);
		if (roleList != null && !roleList.isEmpty()) {
			for (SysRole role : roleList) {
				List<SysAuth> roleAuths = authRepo.findByPObjIdAndPObjTypeAndMenuId(role.getRoleId(), "R",
						menu.getMenuId());
				for (SysAuth auth : roleAuths) {
					if ("Y".equals(auth.getpType())) {
						isAuth = true;
					} else if ("N".equals(auth.getpType())) {
						isAuth = false;
						return isAuth;
					}
				}
			}
		}

		return isAuth;
	}

}
